﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class CourseDetail : System.Web.UI.PageEx
{
    private DataSet ds = null;
    private string strSelectId = string.Empty;
    private string strSelectClassId = string.Empty;
    private string strBigSelectId = string.Empty;
    private string strSmallSelectId = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        strSelectId = Convert.ToString(Request.QueryString["Id"]);
        strSelectClassId = Convert.ToString(Request.QueryString["ClassId"]);
        string strTempSelectId = strSelectClassId.StartsWith("|0|") ? strSelectClassId.Substring(2) : strSelectClassId;
        strTempSelectId = strTempSelectId.Trim('|');
        string[] arrSelectId = strTempSelectId.Split('|');
        strBigSelectId = arrSelectId[0];
        strSmallSelectId = arrSelectId.Length > 0 ? arrSelectId[arrSelectId.Length - 1] : arrSelectId[0];
        if (!Page.IsPostBack)
        {
            DataBinds();
        }
    }
    private void DataBinds()
    {
        int iSelectId = 0, iClassSelectId = 0;
        int.TryParse(strSmallSelectId, out iClassSelectId);
        Model.DbClass dbClass = DalEx.DbClassEx.Select(iClassSelectId);
        if (dbClass != null)
            ltShowSubject0.Text = dbClass.CName;
        //绑定左侧类别菜单
        ds = DalEx.DbClassEx.Select(" and cStatus=1 and cType=2 and cLevel=0 order by cOrder asc,cId desc");
        rptClassMenu.DataSource = ds;
        rptClassMenu.DataBind();
        int.TryParse(strSelectId, out iSelectId);
        Model.DbArticles dbArticles = DalEx.DbArticlesEx.Select(iSelectId);
        Model.DbArticles _dbArticles = null;
        if (dbArticles != null)
        {
            this.Title = dbArticles.ATitle;
            lblPubTime.Text = dbArticles.AAddTime.ToString("yyyy-MM-dd HH:mm:ss");
            litTitle.Text = string.IsNullOrEmpty(dbArticles.AHtmlTitle) ? dbArticles.ATitle : dbArticles.AHtmlTitle;
            litContents.Text = HttpUtility.HtmlDecode(dbArticles.ANote);
            setMeta(dbArticles.ATitle, dbArticles.AKeywords, dbArticles.ADescription);
        }
        else
        {
            MessageBox1("抱歉，信息不存在或已被删除！", "/Default.aspx");
        }
    }
    protected void rptClassMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Repeater rptSubClassMenu = (Repeater)e.Item.FindControl("rptSubClassMenu");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            ds = DalEx.DbClassEx.Select(" and cStatus=1 and cType=2 and cUpId=" + Convert.ToInt32(drv["cId"]) + " order by cOrder asc,cId desc");
            rptSubClassMenu.DataSource = ds;
            rptSubClassMenu.DataBind();

            HtmlGenericControl dtClass = (HtmlGenericControl)e.Item.FindControl("dtClass");
            HtmlGenericControl ddClass = (HtmlGenericControl)e.Item.FindControl("ddClass");
            if (Convert.ToString(drv["cId"]) == strBigSelectId)
            {
                dtClass.Attributes.Add("class", "current");
                ddClass.Attributes.Add("style", "display: block;");
            }
            else
            {
                dtClass.Attributes.Add("class", "");
                ddClass.Attributes.Add("style", "display: none;");
            }
        }
    }
    private void setMeta(string title, string key, string des)
    {
        string strWebTitle = string.Empty, strWebKeywords = string.Empty, strWebDescription = string.Empty;
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            strWebTitle = dbSystem.STitle;
            strWebKeywords = dbSystem.SKeywords;
            strWebDescription = dbSystem.SDescription;
        }
        HtmlMeta keywords = new HtmlMeta(); // keywords
        HtmlMeta description = new HtmlMeta(); // description
        keywords.Name = "keywords";
        keywords.Content = string.IsNullOrEmpty(key) ? strWebKeywords : key;

        this.Title = string.IsNullOrEmpty(title) ? strWebTitle : title;

        description.Name = "description";
        description.Content = string.IsNullOrEmpty(des) ? strWebDescription : des;

        Page.Header.Controls.Add(keywords);
        Page.Header.Controls.Add(description);
    }
}
